﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mathhammer40K.Data;

namespace Mathhammer40K
{
    public partial class FormAddUnit : Form
    {
        CombatInputs.UnitsRow newUnit;

        public FormAddUnit(CombatInputs.UnitsRow row)
        {
            newUnit = row;
            InitializeComponent();

            comboSide.SelectedIndex = 0;

            // Set values into row
            if (!newUnit.IsNameNull())
            {
                textName.Text = newUnit.Name;
                buttonAdd.Text = "Confirm";
            }

            if(!newUnit.IsSideNull())
                comboSide.SelectedIndex = newUnit.Side - 1;

            if(!newUnit.IsCountNull())
                textCount.Text = newUnit.Count.ToString();

            if(!newUnit.IsWeaponSkillNull())
                textWS.Text = newUnit.WeaponSkill.ToString();
            if(!newUnit.IsBallisticSkillNull())
                textBS.Text = newUnit.BallisticSkill.ToString();            
            if(!newUnit.IsStrengthNull())
                textS.Text = newUnit.Strength.ToString();

            if(!newUnit.IsToughnessNull())
                textT.Text = newUnit.Toughness.ToString();
            
            if(!newUnit.IsWoundsNull())
                textW.Text = newUnit.Wounds.ToString();

            if(!newUnit.IsInitiativeNull())
                textI.Text = newUnit.Initiative.ToString();

            if(!newUnit.IsAttacksNull())
                textA.Text = newUnit.Attacks.ToString();

            if(!newUnit.IsLeadershipNull())
                textLd.Text = newUnit.Leadership.ToString();

            if(!newUnit.IsArmorSaveNull())
                textSv.Text = newUnit.ArmorSave.ToString();

            if (!newUnit.IsInvulnerableSaveNull())
                textSv2.Text = newUnit.InvulnerableSave.ToString();

            if (!newUnit.IsWeaponStrengthNull())
                textWeaponStrength.Text = newUnit.WeaponStrength.ToString();

            if (!newUnit.IsWeaponAPNull())
                TextWeaponAP.Text = newUnit.WeaponAP.ToString();

            if (!newUnit.IsWeaponShotsNull())
                textShots.Text = newUnit.WeaponShots.ToString();

            /////////////////////////////////////////////
            // Special Values
            Int32 specialRules = newUnit.SpecialRules;

            if ((specialRules & (int)CombatEngine.SpecialRules.AP2) != 0)
                checkAP2.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.PowerWeapon) != 0)
                checkPowerWeapon.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.PowerFist) != 0)
                checkPoweFist.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.ThunderHammer) != 0)
                checkThunderHammer.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.PowerMaul) != 0)
                checkPowerMaul.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.PowerAxe) != 0)
                checkPowerAxe.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.Charge) != 0)
                checkCharge.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.CounterAttack) != 0)
                checkCounterAttack.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.TwoCCWeapons) != 0)
                checkTwoCCWeapons.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.Rage) != 0)
                checkRage.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.FuriousCharge) != 0)
                checkFuriousCharge.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.FeelNoPain) != 0)
                checkFeelNoPain.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.Overwatch) != 0)
                checkOverwatch.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.Template) != 0)
                checkTemplateWeapon.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.HammerOfWrath) != 0)
                checkHammerOfWrath.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.RerollHits) != 0)
                checkRerollHits.Checked = true;
            if ((specialRules & (int)CombatEngine.SpecialRules.RerollWounds) != 0)
                checkRerollWounds.Checked = true;

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            newUnit.Name = "INVALID";
            this.Close();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            // Set values into row
            newUnit.Name = textName.Text;

            Int32 value = 0;
            if (!Int32.TryParse(comboSide.ValueMember, out value))
                value = 1;
            newUnit.Side = value;

            if (!Int32.TryParse(textCount.Text, out value))
                value = 0;
            newUnit.Count = value;

            if (!Int32.TryParse(comboSide.Text, out value))
                value = 1;
            newUnit.Side = value;


            
            if (!Int32.TryParse(textWS.Text, out value))
                value = 0;
            newUnit.WeaponSkill = value;

            if (!Int32.TryParse(textBS.Text, out value))
                value = 0;
            newUnit.BallisticSkill = value;

            if (!Int32.TryParse(textS.Text, out value))
                value = 0;
            newUnit.Strength = value;
            
            if (!Int32.TryParse(textT.Text, out value))
                value = 0;
            newUnit.Toughness = value;

            if (!Int32.TryParse(textW.Text, out value))
                value = 0;
            newUnit.Wounds = value;

            if (!Int32.TryParse(textW.Text, out value))
                value = 0;
            newUnit.Wounds = value;

            if (!Int32.TryParse(textI.Text, out value))
                value = 0;
            newUnit.Initiative = value;

            if (!Int32.TryParse(textA.Text, out value))
                value = 0;
            newUnit.Attacks = value;

            if (!Int32.TryParse(textLd.Text, out value))
                value = 0;
            newUnit.Leadership = value;

            if (!Int32.TryParse(textSv.Text, out value))
                newUnit.SetArmorSaveNull();
            else
                newUnit.ArmorSave = value;

            if (!Int32.TryParse(textSv2.Text, out value))
                newUnit.SetInvulnerableSaveNull();
            else
                newUnit.InvulnerableSave = value;

            if (!Int32.TryParse(textWeaponStrength.Text, out value))
                value = 0;
            newUnit.WeaponStrength = value;

            if (!Int32.TryParse(TextWeaponAP.Text, out value))
                value = 0;
            newUnit.WeaponAP = value;

            if (!Int32.TryParse(textShots.Text, out value))
                value = 0;
            newUnit.WeaponShots = value;

            /////////////////////////////////////////////
            // Special Values
            Int32 specialRules = (int)CombatEngine.SpecialRules.None;

            if (checkAP2.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.AP2;
            if (checkPowerWeapon.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.PowerWeapon;
            if (checkPoweFist.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.PowerFist;
            if (checkThunderHammer.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.ThunderHammer;
            if (checkPowerMaul.Checked)                
                specialRules = specialRules | (int)CombatEngine.SpecialRules.PowerMaul;
            if (checkPowerAxe.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.PowerAxe;
            if (checkCharge.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.Charge;
            if (checkCounterAttack.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.CounterAttack;
            if (checkTwoCCWeapons.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.TwoCCWeapons;
            if (checkRage.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.Rage;
            if (checkFuriousCharge.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.FuriousCharge;
            if (checkFeelNoPain.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.FeelNoPain;
            if (checkOverwatch.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.Overwatch;
            if (checkTemplateWeapon.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.Template;
            if (checkHammerOfWrath.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.HammerOfWrath;
            if (checkRerollHits.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.RerollHits;
            if (checkRerollWounds.Checked)
                specialRules = specialRules | (int)CombatEngine.SpecialRules.RerollWounds;

            newUnit.SpecialRules = specialRules;

            this.Close();
        }

        private void checkTemplateWeapon_CheckedChanged(object sender, EventArgs e)
        {
            if(checkTemplateWeapon.Checked)
                textShots.Text = "0";
            textShots.Enabled = !checkTemplateWeapon.Checked;
        }
    }
}
